package com.joe.bibackend.utils;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationOutput;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;

import java.util.HashMap;
import java.util.Map;

public class AIUtils {
    public static Map<String,String> askAliChart(String request){
        Map<String,String> map = new HashMap<>();
        Constants.apiKey="sk-af09fa7477c34465959b5ec05bde0ee9";
        Generation gen = new Generation();
        MessageManager msgManager = new MessageManager(5);
        Message systemMsg =
                Message.builder().role(Role.SYSTEM.getValue()).content("你是一名专业的数据分析师").build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content(request).build();
        msgManager.add(systemMsg);
        msgManager.add(userMsg);
        QwenParam param =
                QwenParam.builder().model(Generation.Models.QWEN_TURBO).messages(msgManager.get())
                        .resultFormat(QwenParam.ResultFormat.MESSAGE)
                        .topP(0.8)
                        .enableSearch(true)
                        .build();
        GenerationResult result = null;
        try {
            result = gen.call(param);
            map.put("echart",result.getOutput().getChoices().get(0).getMessage().getContent());
            msgManager.add(result);
            param.setPrompt("请对该数据做简单现状和趋势分析。");
            param.setMessages(msgManager.get());
            result = gen.call(param);
            map.put("analysis",result.getOutput().getChoices().get(0).getMessage().getContent());
        } catch (NoApiKeyException | InputRequiredException e) {
            e.printStackTrace();
        }
        System.out.println(map);
        return map;
    }
}
